package com.example.villagemarket.adapter;

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.example.villagemarket.R;
import com.example.villagemarket.ShoppingCart;
import com.example.villagemarket.R.drawable;
import com.example.villagemarket.R.id;
import com.example.villagemarket.R.layout;
import com.example.villagemarket.ViewShoppingCart;
import com.haarman.listviewanimations.itemmanipulation.ExpandableListItemAdapter;
import com.haarman.listviewanimations.swinginadapters.prepared.ScaleInAnimationAdapter;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ShoppingCartAdapter extends ExpandableListItemAdapter<HashMap<String, String>> {

	private Activity activity;
	private List<HashMap<String, String>> data;
	private static LayoutInflater inflater = null;
	Context context;
	HashMap<String, String> merchandise;
	






	
	
	
	
	public ShoppingCartAdapter(Context c, List<HashMap<String, String>> items, Activity a) {
		super(c, items);
		
		activity = a;
		data = items;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		context = c;
		
		merchandise = new HashMap<String, String>();
		
		setLimit(1);
		

		


	}






	public int getCount() {
		return data.size();
	}

	public HashMap<String, String> getItem(HashMap<String, String> position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
	    View view =  super.getView(position, convertView, parent);
	    //view.setTag(new Integer(position));
	    view.setOnLongClickListener(new OnLongClickListener(){

			@Override
			public boolean onLongClick(final View arg0) {
				
				AlertDialog.Builder builder = new AlertDialog.Builder(activity);

			    builder.setTitle("Confirm Deletion");
			    builder.setMessage("This action cannot be undone");

			    builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {

			        public void onClick(DialogInterface dialog, int which) {
			        	//This is used to get the position of the list view item that was long clicked
			        	TextView description = (TextView) arg0.findViewById(R.id.description);

			        	int psn = (Integer)description.getTag();
						ShoppingCart.items.remove(data.get(psn).get("description"));
						data.remove(psn);
						notifyDataSetChanged();		

						
						ViewShoppingCart.updateTotal();


			            dialog.dismiss();
			        }

			    });

			    builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {

			        @Override
			        public void onClick(DialogInterface dialog, int which) {
			            // Do nothing
			            dialog.dismiss();
			        }
			    });

			    AlertDialog alert = builder.create();
			    alert.show();
			    

				return false;
			}
	    	
	    });
	    return view;
	}

    @Override
    public View getTitleView(int position, View convertView, ViewGroup parent) {
    	
    	View vi = convertView;
		if (convertView == null) {
			vi = inflater.inflate(R.layout.shopping_cart_item, null);
		}

		TextView description = (TextView) vi.findViewById(R.id.description);
		description.setTag(new Integer(position));
		
		merchandise = data.get(position);
		
		TextView imageAmount = (TextView) vi.findViewById(R.id.amount);
		//imageAmount.setTag(vi.findViewById(R.id.amount));
		imageAmount.setTag(new Integer(position));
		imageAmount.setText(ShoppingCart.items.get(merchandise.get(ResultListView.ITEM_DESCRIPTION)).get(0));
		
		
		
		imageAmount.setOnClickListener(new OnClickListener(){
			
			
			
			@Override
			public void onClick(final View arg0) {
				
				
				final EditText input = new EditText(activity);
				input.setInputType(InputType.TYPE_CLASS_NUMBER);
				
				new AlertDialog.Builder(activity)
			    .setTitle("Update Item Amount In Cart")
			    .setView(input)
			    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			        public void onClick(DialogInterface dialog, int whichButton) {
			            Editable value = input.getText(); 
			            int psn = (Integer)arg0.getTag();
			            
						ArrayList<String> temp = ShoppingCart.items.get(data.get(psn).get("description"));

						//quantity
						temp.set(0, (Integer.parseInt(value.toString())) + ""); 
						
						ShoppingCart.items.put(data.get(psn).get("description"), temp);

						HashMap<String,String> temp1 = data.get(psn);
						temp1.put("quantity", (Integer.parseInt(value.toString())) + "");
						data.set(psn, temp1);

						notifyDataSetChanged();		
						
						ViewShoppingCart.updateTotal();
			            
			        }
			    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			        public void onClick(DialogInterface dialog, int whichButton) {
			            // Do nothing.
			        }
			    }).show();
				


			}
			
		});
		

		description.setText(merchandise.get(ResultListView.ITEM_DESCRIPTION));

		return vi;

    }
    
    public boolean isInteger(String s) {
        try { 
            Integer.parseInt(s); 
        } catch(NumberFormatException e) { 
            return false; 
        }
        return true;
    }
    

    @Override
    public View getContentView(int position, View convertView, ViewGroup parent) {
    	
    	View vi = convertView;
		if (convertView == null) {
			vi = inflater.inflate(R.layout.shopping_cart_expanded_item, null);
		}
		
		TextView price = (TextView) vi.findViewById(R.id.price);
		price.setTag(new Integer(position));
		
		TextView quantity = (TextView) vi.findViewById(R.id.quantity);
		quantity.setTag(new Integer(position));
		
		TextView total = (TextView) vi.findViewById(R.id.total);
		total.setTag(new Integer(position));
		
		//first one is quantity, second one is price
		String q = ShoppingCart.items.get(merchandise.get(ResultListView.ITEM_DESCRIPTION)).get(0);
		String p = ShoppingCart.items.get(merchandise.get(ResultListView.ITEM_DESCRIPTION)).get(1);
		BigDecimal qTemp = new BigDecimal(q);
		BigDecimal pTemp = new BigDecimal(p);
				
		
		BigDecimal t = qTemp.multiply(pTemp);
		
		price.setText("Price: "+ p);
		quantity.setText("Amt in Cart: " + q);
		total.setText("Total: " + t);

        return vi;
    }
    








}